﻿/**
 * Created by Administrator on 2017/11/27.
 */
$(function () {
	
	new Swiper('#swiper-list', {
        pagination:{
            el:'.swiper-pagination',
            type:'bullets',
            clickable:'true',
        },
        paginationClickable: true,
        paginationType:'bullets',
        loop: true,
        autoplay:{
            delay:2500,
            disableOnInteraction: false,
        }
    });
    new Swiper('#package-ul', {
        pagination: '.swiper-pagination',
        slidesPerView: 'auto',
        paginationClickable: true,
    });
    
    $(".pattern-title>li").on("click", function () {
        $(this).addClass("active").siblings().removeClass("active");
        if($(this).index()==0){
            $.each($(".swiper-wrapper li img"), function (index) {
                $(this).parents("a").attr("href","http://www.efubao.com/huodong/20.html#tuanjian");
                var thissrc=$(this).attr("t-src");
                $(this).attr("src",thissrc);
                $(this).next().html("T-0"+(index+1))
            })
        }else if($(this).index()==1){
            $.each($(".swiper-wrapper li img"), function (index) {
                $(this).parents("a").attr("href","http://www.efubao.com/huodong/20.html#juhui");
                var thissrc=$(this).attr("j-src");
                $(this).attr("src",thissrc);
                $(this).next().html("J-0"+(index+1))
            })
        }else if($(this).index()==2){
            $.each($(".swiper-wrapper li img"), function (index) {
                $(this).parents("a").attr("href","http://www.efubao.com/huodong/20.html#biye");
                var thissrc=$(this).attr("b-src");
                $(this).attr("src",thissrc);
                $(this).next().html("B-0"+(index+1))
            })
        }else if($(this).index()==3){
            $.each($(".swiper-wrapper li img"), function (index) {
                $(this).parents("a").attr("href","http://www.efubao.com/huodong/20.html#nianhui");
                var thissrc=$(this).attr("n-src");
                $(this).attr("src",thissrc);
                $(this).next().html("N-0"+(index+1))
            })
        }
    })
    
    var demandNum=$(".num-box").attr("demandNumber");
    var demandHtml="";
    demandNum=demandNum+"";
    for(var i=0;i<demandNum.length;i++){
        demandHtml=demandHtml+'<span class="num">'+demandNum.substring(i,i+1)+'</span>';
    }
    $(".num-box").append(demandHtml);

    var getAuth=new getAuthCode($(".sms-code-div"),Mall.home+"/authCode/getSMSCode",1);
    getAuth.init();
    
    $(".pop-bottom").on("click", function () {
    	initDemandLayout();
        $(this).parents(".pop-div").removeClass("show");
        window.location.reload();
    })
    
    $(".demand-submit").on("click", function(e) {
		var fn = arguments.callee, self = this;  
		$(self).unbind('click', fn);
		var isLogin = $(".save-demand-form").attr("isLogin")==1?true:false;
		if(isLogin){
    		saveDemand(cookie.get("mobile"),self,fn);
    	}else{
    		var phoneNum=$("#mobile-ipt").val();
    		if(phoneNum=="" || phoneNum==null){
    			showTip("请输入手机号");
    			$(self).click(fn); 
            	return;
            }else if(!(/^1[34578]\d{9}$/.test(phoneNum))){
            	showTip("手机号码错误");
            	$(self).click(fn); 
            	return;
            }
    		var validCode = $("#vaid-code").val();
    		if (validCode == "" || validCode == null) {
    			showTip("请输入验证码");
    			$(self).click(fn); 
    			return;
    		}
    		doLoginAndSaveDemand(phoneNum,validCode,self,fn);
    	} 
	})
    initDemandLayout();
})

// 判断用户是否登陆，用于初始化提需求布局
function initDemandLayout(){
    $.ajax({
        url: Mall.home+'/api/user/verifytoken',
        type : 'GET',
        xhrFields : {
            withCredentials : true
        },
        data:{
        	token:cookie.get("token")
        },
        crossDomain : true,
        dataType: "jsonp",
        success:function(data){
            if(data.code != "200"){
                // 未登录
                $(".sms-code-div").show();
                $(".save-demand-form").attr("isLogin", 0);
            }else{
            	$(".sms-code-div").hide();
                $(".save-demand-form").attr("isLogin", 1);
            }
        }
    })
}

/**
 * 登录用户提交需求
 * @param phoneNum
 * @param customizedDesc
 */
function saveDemand(phoneNum,self,fn){
	var sourceUrl,sourceTitle,sourceName;
	if (cookie.get("sourceName") != undefined) {
		sourceName = decodeURIComponent(cookie.get("sourceName"));
	}else{
		sourceName = $("title").html();
	}
	if (cookie.get("sourceTitle") != undefined) {
		sourceTitle = decodeURIComponent(cookie.get("sourceTitle"));
	}else{
		sourceTitle = $("title").html();
	}
	if (cookie.get("sourceUrl") != undefined) {
		sourceUrl = decodeURIComponent(cookie.get("sourceUrl"));
	}else{
		sourceUrl =  window.location.href;
	}
	var customizedDesc =  $("#customizedDesc").val();
	var saveData = {
	        "demandUserMobile": phoneNum,
	        "customizedDesc": customizedDesc,
	        "reqSource": "2",
	        "reqType": "5",
	        "sourceName": sourceName,
	        "sourceTitle": sourceTitle,
	        "sourceUrl": window.location.href,
	        "sourceBtnName":  $(".demand-submit").html(),
	        "sourceReferrerUrl":sourceUrl
	    };
	    $.ajax({
	        url: Mall.home + "/api/req/add",
	        type: "POST",
	        dataType: "json",
	        contentType: "application/json",
	        xhrFields : {
	            withCredentials : true
	        },
	        crossDomain : true,
	        data: JSON.stringify(saveData),
	        success: function(data) {
	        	if (data.code == 200) {
	    			$(".pop-success").addClass("show");
	    		} else if (data.code == 999) {
	    			$(".pop-tip").addClass("show");
	    		} else {
	    			$(".pop-fail").addClass("show");
	    		}
	        	$(self).click(fn);
	        }
	    })
}

/**
 * 自动注册登录并提交需求
 * @param phoneNum
 * @param validCode
 * @param customizedDesc
 */
function doLoginAndSaveDemand(phoneNum,validCode,self,fn){
	$.ajax({
        url: Mall.home + '/user/loginOrAutoRegister',
        dataType: "jsonp",
        async: false,
        data: {
            "mobile": phoneNum,
            "smsAuthCode": validCode
        },
        success: function(data) {
            if (data.code == 0) {
                setLoginCookie(data.data.token,data.data.userId,data.data.mobile)
            	saveDemand(phoneNum,self,fn)
            }else{
            	 $(self).click(fn);
            }
        }
    });
}